package com.mezz.bones.framework.base.util;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Author: zhuang
 * @Date: 2025/6/11 16:47
 * @Description: TODO
 * @Version: 1.0
 **/

public class IdUtil {

    private final static ConcurrentHashMap<String, AtomicLong> bizCounters = new ConcurrentHashMap<>();

    /**
     * 为指定业务key生成下一个自增ID（从1开始递增）
     * @param businessKey 业务标识（如"order"、"notification"）
     * @return 业务key对应的自增ID
     */
    public static long nextId(String businessKey) {
        // 若业务key不存在，初始化计数器为0（首次调用时 incrementAndGet() 会返回1）
        AtomicLong counter = bizCounters.computeIfAbsent(businessKey, k -> new AtomicLong(0));
        return counter.incrementAndGet();
    }
}
